<template>
  <div class="admin-layout">
    <el-container>
      <!-- 侧边栏 -->
      <el-aside :width="isCollapse ? '64px' : '200px'" class="sidebar">
        <div class="logo">
          <img v-if="!isCollapse" src="/favicon.ico" alt="Logo" class="logo-img" />
          <span v-if="!isCollapse" class="logo-text">eSIM Store</span>
          <img v-else src="/favicon.ico" alt="Logo" class="logo-img-mini" />
        </div>

        <el-menu
          :default-active="activeMenu"
          class="sidebar-menu"
          :collapse="isCollapse"
          :unique-opened="true"
          router
        >
          <template v-for="menu in filteredMenus" :key="menu.id">
            <!-- 单级菜单 -->
            <el-menu-item v-if="!menu.children || menu.children.length === 0" :index="menu.path">
              <el-icon v-if="menu.icon">
                <component :is="menu.icon" />
              </el-icon>
              <template #title>{{ menu.title }}</template>
            </el-menu-item>

            <!-- 多级菜单 -->
            <el-sub-menu v-else :index="menu.id">
              <template #title>
                <el-icon v-if="menu.icon">
                  <component :is="menu.icon" />
                </el-icon>
                <span>{{ menu.title }}</span>
              </template>
              <el-menu-item
                v-for="child in menu.children"
                :key="child.id"
                :index="child.path"
              >
                {{ child.title }}
              </el-menu-item>
            </el-sub-menu>
          </template>
        </el-menu>
      </el-aside>

      <!-- 主内容区 -->
      <el-container>
        <!-- 顶部导航 -->
        <el-header class="header">
          <div class="header-left">
            <el-button
              type="text"
              @click="toggleCollapse"
              class="collapse-btn"
            >
              <el-icon><Expand v-if="isCollapse" /><Fold v-else /></el-icon>
            </el-button>

            <el-breadcrumb separator="/">
              <el-breadcrumb-item
                v-for="item in breadcrumbs"
                :key="item.path"
                :to="item.path"
              >
                {{ item.title }}
              </el-breadcrumb-item>
            </el-breadcrumb>
          </div>

          <div class="header-right">
            <el-dropdown @command="handleCommand">
              <span class="user-info">
                <el-avatar :size="32" :src="userInfo.avatar">
                  {{ userInfo.name.charAt(0) }}
                </el-avatar>
                <span class="username">{{ userInfo.name }}</span>
                <el-icon class="el-icon--right"><ArrowDown /></el-icon>
              </span>
              <template #dropdown>
                <el-dropdown-menu>
                  <el-dropdown-item command="profile">个人中心</el-dropdown-item>
                  <el-dropdown-item command="settings">设置</el-dropdown-item>
                  <el-dropdown-item divided command="logout">退出登录</el-dropdown-item>
                </el-dropdown-menu>
              </template>
            </el-dropdown>
          </div>
        </el-header>

        <!-- 内容区域 -->
        <el-main class="main-content">
          <router-view />
        </el-main>
      </el-container>
    </el-container>
  </div>
</template>

<script setup lang="ts">
import { ref, computed } from 'vue'
import { useRouter } from 'vue-router'
import { ElMessage } from 'element-plus'
import { useUserStore } from '@/stores/counter'
import { useMenu } from '@/composables/useMenu'

const router = useRouter()
const userStore = useUserStore()
const { filteredMenus, activeMenu, breadcrumbs } = useMenu()

// 侧边栏折叠状态
const isCollapse = ref(false)

// 用户信息
const userInfo = computed(() => userStore.userInfo || {
  name: '管理员',
  avatar: ''
})

// 切换侧边栏折叠状态
const toggleCollapse = () => {
  isCollapse.value = !isCollapse.value
}

// 处理用户下拉菜单命令
const handleCommand = (command: string) => {
  switch (command) {
    case 'profile':
      router.push('/profile')
      break
    case 'settings':
      router.push('/settings')
      break
    case 'logout':
      ElMessage.success('退出登录成功')
      router.push('/login')
      break
  }
}
</script>

<style lang="scss" scoped>
.admin-layout {
  height: 100vh;

  .el-container {
    height: 100%;
  }

  .sidebar {
    background-color: #304156;
    transition: width 0.3s;

    .logo {
      height: 60px;
      display: flex;
      align-items: center;
      justify-content: center;
      background-color: #2b3a4b;

      .logo-img {
        width: 32px;
        height: 32px;
        margin-right: 8px;
      }

      .logo-img-mini {
        width: 32px;
        height: 32px;
      }

      .logo-text {
        color: #fff;
        font-size: 18px;
        font-weight: bold;
      }
    }

    .sidebar-menu {
      border: none;
      background-color: #304156;

      :deep(.el-menu-item) {
        color: #bfcbd9;

        &:hover {
          background-color: #263445;
          color: #409eff;
        }

        &.is-active {
          background-color: #409eff;
          color: #fff;
        }
      }

      :deep(.el-sub-menu__title) {
        color: #bfcbd9;

        &:hover {
          background-color: #263445;
          color: #409eff;
        }
      }

      :deep(.el-sub-menu .el-menu-item) {
        background-color: #1f2d3d;

        &:hover {
          background-color: #001528;
        }

        &.is-active {
          background-color: #409eff;
        }
      }
    }
  }

  .header {
    background-color: #fff;
    border-bottom: 1px solid #e4e7ed;
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 0 20px;

    .header-left {
      display: flex;
      align-items: center;

      .collapse-btn {
        margin-right: 20px;
        font-size: 18px;
      }
    }

    .header-right {
      .user-info {
        display: flex;
        align-items: center;
        cursor: pointer;

        .username {
          margin: 0 8px;
          color: #606266;
        }
      }
    }
  }

  .main-content {
    background-color: #f0f2f5;
    padding: 20px;
  }
}
</style>
